package com.xiaochong.tir.manager.controller;

import com.github.pagehelper.PageInfo;
import com.xiaochong.service.common.config.oss.OSSTool;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.data.po.Splash;
import com.xiaochong.tir.common.util.RedisKeysUtils;
import com.xiaochong.tir.manager.service.SplashService;
import com.xiaochong.tir.manager.util.ManagerVersion;
import com.xiaochong.tir.manager.util.ReturnResult;
import com.xiaochong.tir.common.data.vo.SplashVo;
import com.xiaochong.tir.manager.util.SessionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

/**
 * @Author: WangXing
 * @Description:
 * @CreateDate: 2018/8/3 9:20
 */
@RestController
@RequestMapping(ManagerVersion.API_V1_PREFIX + "splash/")
@Api("App splash")
public class SplashController {

    @Autowired
    private SplashService splashService;

    @Autowired
    private OSSTool ossTool;

    @Autowired
    private RedisServer redisServer;
    /**
     * splash列表页面
     * @return
     */
    @GetMapping("splPage")
    public Object splPage(){
        return new ModelAndView("splash/splashPage");
    }

    /**
     * 查询splash列表
     * @param vo
     * @return
     */
    @ApiOperation(value = "查询splash列表",notes = "查询splash列表")
    @PostMapping("splList")
    public Object splashList(SplashVo vo){
        PageInfo<Splash> info = splashService.selectBySplashList(vo);
        for (int i = 0; i < info.getList().size(); i++) {
            Splash splash = info.getList().get(i);
            if(null != splash){
                if(StringUtils.isNotBlank(splash.getSplashImg())){
                    splash.setSplashImg(ossTool.getOSSUrl(splash.getSplashImg()));
                }
            }
        }
        return new ReturnResult<Splash>(0,"成功",info.getList(),info.getTotal());
    }

    /**
     * 新增splash页面
     * @return
     */
    @GetMapping("addspl")
    public Object addsplPage(){
        return new ModelAndView("splash/addsplPage");
    }

    /**
     * 新增splash方法
     * @param splash
     * @return
     */
    @ApiOperation(value = "新增splash",notes = "新增splash")
    @PostMapping("insSpl")
    public Object insSplash(Splash splash){
        redisServer.set(RedisKeysUtils.LOGINNAME,"新增splash");
        Splash sel = new Splash();
        sel.setSplashName(splash.getSplashName());
        sel.setRedirectUrl(splash.getRedirectUrl());
        Splash old = splashService.selectByEntity(sel);
        if(null!= old){
            return new ReturnResult<Splash>(-1,"重复添加",1,1);
        }
        splash.setCreateTime(new Date());
        splash.setEnableFlag(false);
        splashService.insertSelective(splash);
        return new ReturnResult<Splash>(0,"成功",1,1);
    }

    /**
     * 修改splash状态
     * @param splash
     * @return
     */
    @ApiOperation(value = "修改splash状态",notes = "修改splash状态")
    @PostMapping("enableSpl")
    public Object enableSplash(Splash splash,@RequestParam("str") Integer flag){
        redisServer.set(RedisKeysUtils.LOGINNAME,"修改splash状态");
        Splash old = splashService.selectByPrimaryKey(splash.getId());
        if(flag.equals(1)){
            if(null != splash.getEnableFlag() && splash.getEnableFlag()){
                splash.setEnableFlag(false);
            }else {
                splash.setEnableFlag(true);
            }
            splash.setUpdateTime(new Date());
            splashService.updateBySplashKey(splash,old);
        }else if(flag.equals(2)){
            splashService.updateBatchs();
            splash.setEnableFlag(true);
            splash.setUpdateTime(new Date());
            splashService.updateBySplashKey(splash,old);
        }
        return new ReturnResult<Splash>(0,"成功",1,1);
    }

    /**
     * 查看Splash
     * @param id
     * @return
     */
    @ApiOperation(value = "查看Splash",notes = "查看Splash")
    @GetMapping("detilSpl")
    public Object detilSplash(@RequestParam("id")Integer id){
        Splash splash = splashService.selectByPrimaryKey(id);
        if(StringUtils.isNotBlank(splash.getSplashImg())){
            splash.setSplashImg(ossTool.getOSSUrl(splash.getSplashImg()));
        }
        ModelAndView mv = new ModelAndView("splash/viewSpl");
        mv.addObject("splash",splash);
        return mv;
    }

    /**
     * Splash提示
     * @return
     */
    @ApiOperation(value = "Splash上架提示",notes = "Splash上架提示")
    @PostMapping("selSplash")
    public Object selSplash(){
        List<Splash> list = splashService.selectByEntityList(new Splash().setEnableFlag(true));
        if(list.size() > 0){
            return new ReturnResult<>(1,"当前线上已有正在使用的Splash《"+list.get(0).getSplashName()+"》，是否确认替换？",list,list.size());
        }else {
            return new ReturnResult<>(0,"",1,1);
        }
    }
}
